//
//  shaiDanViewController.m
//  YuErmuYing
//
//  Created by Tiancheng on 16/8/13.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "shaiDanViewController.h"
#import "HttpRequest.h"
#import "shaiDanTableViewCell.h"
#import "Macros.h"
#import "shaiDan.h"
#import "UIImageView+WebCache.h"
#import "UILabel+LabelHeightAndWidth.h"
#import "NSString+NSDateToNSString.h"
#import "yiYuanDuoBaoViewController.h"
#import "goodsDetailViewController.h"
#import "homeViewController.h"

#import "SVProgressHUD.h"
@interface shaiDanViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIView *headerView01;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (weak, nonatomic) IBOutlet UIButton *joinButton;
@property(nonatomic,copy)NSMutableArray *dataArray;
@property(nonatomic,retain)shaiDanTableViewCell*myCell;
@property(nonatomic,assign)float picImageView_y;
//晒单里的图片分享
@property(nonatomic,retain)UIImageView *imageView;
@end

@implementation shaiDanViewController
@synthesize dataArray,myCell,picImageView_y,imageView;
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden=YES;
    self.tabBarController.tabBar.hidden=YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBackGroundColor];
    dataArray=[NSMutableArray array];
    self.myTableView.delegate=self;
    self.myTableView.dataSource=self;
    //从服务器获取数据
    [self getDataFromNet];
    [SVProgressHUD showWithStatus:@"加载中请稍后..."];
    self.myTableView.hidden=YES;
}
//设置背景色
-(void)setBackGroundColor
{
    self.headerView.backgroundColor=[UIColor colorWithRed:244/255.0 green:142/255.0 blue:177/255.0 alpha:1];
    self.headerView01.backgroundColor=[UIColor colorWithRed:240/255.0 green:98/255.0 blue:146/255.0 alpha:1];
    [self.joinButton setBackgroundColor:pinkColor];
    //添加点击方法
    [self.joinButton addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
}
//从服务器获取数据
-(void)getDataFromNet
{
    NSString*urlString=[NSString stringWithFormat:@"http://112.74.114.1/Parenting/index.php/api/IndShine/shine"];
    //http://112.74.114.1/Parenting/index.php/api/IndShine/shine
    HttpRequest *httpRequest=[HttpRequest sharedInstance];
    [httpRequest postWithURLString:urlString parameters:nil success:^(id responseObject)
     {
         id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
         dataArray=[json objectForKey:@"data"];
         if (dataArray.count>0)
         {
             [self dismiss];
         }
//         NSLog(@"json=%@",json);
     } failure:^(NSError *error) {
         NSLog(@"error=%@",error);
     }];
}
- (void)dismiss
{
    self.myTableView.hidden=NO;
    [SVProgressHUD dismiss];
    [self.myTableView reloadData];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    shaiDan *sd=[shaiDan shardeManager];
    static NSString *identifier = @"myCell";
    self.myCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!self.myCell)
    {
        self.myCell=[[[NSBundle mainBundle] loadNibNamed:@"shaiDanTableViewCell" owner:self options:nil]lastObject];
        sd.touXiangPic=[dataArray[indexPath.row] objectForKey:@"upic"];
        sd.picString01=[dataArray[indexPath.row] objectForKey:@"pic1"];
        sd.picString02=[dataArray[indexPath.row] objectForKey:@"pic2"];
        sd.picString03=[dataArray[indexPath.row] objectForKey:@"pic3"];
//        if (sd.picString01==NULL)
//        {
//            NSLog(@"图片为空");
//        }
        sd.timeString=[dataArray[indexPath.row] objectForKey:@"time"];
        sd.touXiangString=[dataArray[indexPath.row] objectForKey:@"username"];
        sd.contentString=[dataArray[indexPath.row] objectForKey:@"content"];
        self.myCell.nameLabel.text=sd.touXiangString;
        self.myCell.contentLabel.text=sd.contentString;
        float height=[UILabel getHeightByWidth:myCell.contentLabel.frame.size.width title:sd.contentString font:[UIFont systemFontOfSize:13.0]];
        self.myCell.contentLabel.frame=CGRectMake(myCell.contentLabel.frame.origin.x, myCell.contentLabel.frame.origin.y,myCell.contentLabel.frame.size.width, height);
        NSURL* imagePath0 = [NSURL URLWithString:sd.touXiangPic];
        //给一张默认图片，先使用默认图片，当图片加载完成后再替换
        [myCell.picImageView sd_setImageWithURL:imagePath0 placeholderImage:[UIImage imageNamed:@"default"]];
        float pic_height=myCell.contentLabel.frame.size.height+myCell.contentLabel.frame.origin.y;
        float pic_y=pic_height+5;
        if (sd.picString01==NULL&&sd.picString02==NULL&&sd.picString03==NULL)
        {
            imageView.hidden=YES;
            picImageView_y=pic_y;
        }else
        {
            imageView.hidden=NO;
            picImageView_y=imageView.frame.origin.y+imageView.frame.size.height+5;
            for (int i=0; i<3; i++)
            {
                imageView=[[UIImageView alloc]initWithFrame:CGRectMake(18+(0.0693*screenWidth+0.24267*screenWidth)*i, pic_y, 0.24267*screenWidth, 0.30667*screenWidth)];
                if (i==0)
                {
                    NSURL* imagePath = [NSURL URLWithString:sd.picString01];
                    //给一张默认图片，先使用默认图片，当图片加载完成后再替换
                    [imageView sd_setImageWithURL:imagePath placeholderImage:[UIImage imageNamed:@"default"]];
                }
                else if (i==1)
                {
                    NSURL* imagePath = [NSURL URLWithString:sd.picString02];
                    //给一张默认图片，先使用默认图片，当图片加载完成后再替换
                    [imageView sd_setImageWithURL:imagePath placeholderImage:[UIImage imageNamed:@"default"]];
                }
                else if (i==2)
                {
                    NSURL* imagePath = [NSURL URLWithString:sd.picString03];
                    //给一张默认图片，先使用默认图片，当图片加载完成后再替换
                    [imageView sd_setImageWithURL:imagePath placeholderImage:[UIImage imageNamed:@"default"]];
                }
                [myCell.contentView addSubview:imageView];
            }
        }
        UILabel *timeLabel=[[UILabel alloc]initWithFrame:CGRectMake(18, picImageView_y, screenWidth-36, 21)];
        NSString*timeString=[NSString StringNSDateToNSString:sd.timeString];
        timeLabel.text=timeString;
        timeLabel.textColor=[UIColor grayColor];
        [myCell.contentView addSubview:timeLabel];
        float timeHeight=timeLabel.frame.size.height+timeLabel.frame.origin.y+10;
        CGRect rect = myCell.frame;
        rect.size.height =timeHeight;
        myCell.frame = rect;
        
        self.myCell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    
    return myCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    self.myCell=[self tableView:tableView cellForRowAtIndexPath:indexPath];
    //    self.myCell=[self tableview];
    return self.myCell.frame.size.height;
}
//颜色恢复
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
//{
//    [[self.myTableView cellForRowAtIndexPath:indexPath].contentView setBackgroundColor:[UIColor whiteColor]];
//    
//}
-(void)onClick:(UIButton*)sender
{
    
    shaiDan *sd=[shaiDan shardeManager];
    sd.whichViewToBack=1;
    yiYuanDuoBaoViewController *yydb=[yiYuanDuoBaoViewController new];
    [self.navigationController pushViewController:yydb animated:YES];
//    NSLog(@"黄色按钮➕");
}
- (IBAction)goBack:(id)sender
{
    shaiDan *sd=[shaiDan shardeManager];
    int num=sd.whichViewToBack;
//    NSLog(@"num=%d",num);
    if (num==0)
    {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
    else if (num==1)
    {
        //跳到指定的界面
        for (UIViewController *controller in self.navigationController.viewControllers)
        {
//            if ([controller isKindOfClass:[yiYuanDuoBaoViewController class]])

            if ([controller isKindOfClass:[homeViewController class]])
            {
                [self.navigationController popToViewController:controller animated:YES];
//                NSLog(@"1111111");
            }
        }
    }
    else if (num==2)
    {
        //跳到指定的界面
        for (UIViewController *controller in self.navigationController.viewControllers)
        {
            if ([controller isKindOfClass:[goodsDetailViewController class]])
            {
                [self.navigationController popToViewController:controller animated:YES];
            }
        }
    }
    else if (num==3)
    {
        //跳到指定的界面
        for (UIViewController *controller in self.navigationController.viewControllers)
        {
            if ([controller isKindOfClass:[yiYuanDuoBaoViewController class]])
            {
                [self.navigationController popToViewController:controller animated:YES];
            }
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
